﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fastyou.Model;
using System.ServiceModel;
using Fastyou.WCF.Proxys;

namespace Fastyou.Win.Client
{
    public partial class frmMain : Form, IDuplexCallback
    {
        IDuplex server;
        public frmMain()
        {
            InitializeComponent();
        }

        //private System.ComponentModel.BackgroundWorker backgroundWorker1;
        //private void InitializeBackgoundWorker()
        //{
        //    backgroundWorker1 = new BackgroundWorker();
        //    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        //    //backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        //}

        private void frmMain_Load(object sender, EventArgs e)
        {
            //InitializeBackgoundWorker();
            InstanceContext instanceContext = new InstanceContext(this);
            //DuplexCallback callback = new DuplexCallback();
            ////    IDuplex wss = new DuplexClient(new System.ServiceModel.InstanceContext(callback), "WSDualHttpBinding_IDuplex");

            server = new DuplexClient(instanceContext, "NetTcpBinding_IDuplex");
            server.Start();
            LoadJobs();

        }
        List<Work> jobs = new List<Work>();
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            jobs = server.Jobs();
            if (listView1.InvokeRequired)
            {
                listView1.Invoke(new MethodInvoker(UpdateListView));
            }
            else
            {
                UpdateListView();
            }
        }

        void LoadJobs()
        {
            jobs = server.Jobs();
            if (listView1.InvokeRequired)
            {
                listView1.Invoke(new MethodInvoker(UpdateListView));
            }
            else
            {
                UpdateListView();
            }
        }
        void UpdateListView()
        {
            listView1.Items.Clear();
            foreach (Work job in jobs)
            {
                ListViewItem item = CreateItem(job);
                listView1.Items.Add(item);
            }
        }
        ListViewItem CreateItem(Work job)
        {
            ListViewItem item = new ListViewItem(new string[] { job.WorkName, job.status });
            item.Tag = job;
            return item;
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                try
                {
                    server.DoWork(item.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        #region IDuplexCallback Members

        public void ChangeStatus(Work job)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Text == job.WorkName)
                {
                    item.SubItems[1].Text = job.status;

                    {
                        if (job.status == "Running")
                        {
                            item.ForeColor = Color.Red;
                        }
                        else
                        {
                            item.ForeColor = Color.Green;
                        }
                    }
                }
            }
        }

        public void AddJobCall(Work job)
        {
            ListViewItem item = CreateItem(job);
            listView1.Items.Add(item);
        }

        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                Work job = new Work();
                job.WorkName = txtName.Text.Trim();

                job.status = "ok";
                server.AddJob(job);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
